package cn.cdtu.tibetplan.service.impl;

import java.util.List;

import cn.cdtu.tibetplan.mapper.UserMapper;
import cn.cdtu.tibetplan.pojo.PageResult;
import cn.cdtu.tibetplan.pojo.User;
import cn.cdtu.tibetplan.pojo.UserExample;
import cn.cdtu.tibetplan.util.Tools;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import cn.cdtu.tibetplan.service.UserService;

import org.springframework.stereotype.Service;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询全部
     */
    @Override
    public List<User> findAll() {
        return userMapper.selectByExample(null);
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<User> page = (Page<User>) userMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    /**
     * 修改
     */
    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public User findOne(Long id) {
        return userMapper.selectByPrimaryKey(id.intValue());
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            userMapper.deleteByPrimaryKey(id.intValue());
        }
    }

    @Override
    public PageResult findPage(User user, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (user != null) {
            if (user.getUsername() != null && user.getUsername().length() > 0) {
                criteria.andUsernameLike("%" + user.getUsername() + "%");
            }
            if (user.getPassword() != null && user.getPassword().length() > 0) {
                criteria.andPasswordLike("%" + user.getPassword() + "%");
            }
            if (user.getLevel() != null && user.getLevel() > 0) {
                criteria.andLevelEqualTo(user.getLevel().intValue());
            }
            if (user.getTelephone() != null && user.getTelephone().length() > 0) {
                criteria.andTelephoneLike("%" + user.getTelephone() + "%");
            }
            if (user.getQq() != null && user.getQq().length() > 0) {
                criteria.andQqLike("%" + user.getQq() + "%");
            }
            if (user.getEmail() != null && user.getEmail().length() > 0) {
                criteria.andEmailLike("%" + user.getEmail() + "%");
            }
            if (user.getSchoolname() != null && user.getSchoolname().length() > 0) {
                criteria.andSchoolnameLike("%" + user.getSchoolname() + "%");
            }
            if (user.getCreatetime() != null && user.getCreatetime().length() > 0) {
                criteria.andCreatetimeLike("%" + user.getCreatetime() + "%");
            }
            if (user.getAddress() != null && user.getAddress().length() > 0) {
                criteria.andAddressLike("%" + user.getAddress() + "%");
            }
            if (user.getGender() != null && user.getGender().length() > 0) {
                criteria.andGenderLike("%" + user.getGender() + "%");
            }
            if (user.getOperator() != null && user.getOperator().length() > 0) {
                criteria.andOperatorLike("%" + user.getOperator() + "%");
            }
            if (user.getOther1() != null && user.getOther1().length() > 0) {
                criteria.andOther1Like("%" + user.getOther1() + "%");
            }
            if (user.getOther2() != null && user.getOther2().length() > 0) {
                criteria.andOther2Like("%" + user.getOther2() + "%");
            }
            if (user.getOther3() != null && user.getOther3().length() > 0) {
                criteria.andOther3Like("%" + user.getOther3() + "%");
            }
        }
        Page<User> page = (Page<User>) userMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public User login(User user) {
        // 密码加密
        user.setPassword(Tools.md5(user.getPassword()));
        // 根据用户名查找用户
        User u = userMapper.selectByUsername(user.getUsername());
        // 如果 u为空
        if (u == null) return null;
        // u 不为空
        if (u.getPassword().equals(user.getPassword()))
            return u;
        else
            return null;
    }

    @Override
    public String register(User user) {
        User u = userMapper.selectByUsername(user.getUsername());
        if (u == null) {
            int result = userMapper.insertSelective(user);
            if (result != 0) {
                System.out.println("Code:" + result);
                return "success";
            } else {
                System.out.println("Code:" + result);
                return "error";
            }
        } else {
            return "用户名已存在";
        }
    }

}
